package com.code.shiro.user.controller;

import com.code.shiro.user.service.UserService;
import com.code.wanmi.entity.wm_user.UserDto;
import com.code.wanmi.entity.wm_user.WmSysMenu;
import com.code.wanmi.entity.wm_user.WmSysRole;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**缺省功能
 * 1.修改菜单权限
*/
@RestController
@RequestMapping("/menu")
@Api(tags = "修改菜单权限")
public class WmSysMenuController {
    @Autowired
    private UserService userService;

    /**
     * 根据角色的权限ID删除权限
     * @return
     */

    @ApiOperation(value="删除权限")
    //参数说明
    @ApiImplicitParams({
            @ApiImplicitParam(name="menu_id" ,value="权限ID" ,required=true ,dataType="List<Long>",paramType="query")})
    //返回说明
    @ApiResponses({
            @ApiResponse(code=200 , message="执行成功")
    })

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    //@RequiresRoles(value = {"admin","root"},logical = Logical.OR)
    public String deleteRole(List<Long> menu_id){
        return userService.deleteRole(menu_id);
    }


    /**
     * 通过角色ID查询对应权限列表
     */

    @ApiOperation(value="查询对应权限")
    //参数说明
    @ApiImplicitParams({

            @ApiImplicitParam(name="role_id" ,value="角色ID" ,required=true ,dataType="Long",paramType="query")})
    //返回说明
    @ApiResponses({
            @ApiResponse(code=200 , message="执行成功")
    })
    @RequestMapping(value = "/roleid",method = RequestMethod.GET)
    public String getidperms(Long role_id){
        String power = userService.getRoleid(role_id);
        return power;
    }

    /**
     * 修改菜单权限信息
     */

    @ApiOperation(value="修改菜单权限")
    //参数说明
    @ApiImplicitParams({

            @ApiImplicitParam(name="wmSysMenu" ,value="菜单对象" ,required=true ,dataType="WmSysMenu",paramType="query")})
    //返回说明
    @ApiResponses({
            @ApiResponse(code=200 , message="执行成功")
    })

    @RequestMapping(value = "/updateMenu",method = RequestMethod.PUT)
    public String updateMenu(WmSysMenu wmSysMenu){
        String resuleMessage = userService.updateMenu(wmSysMenu);
        return resuleMessage;
    }

    /**添加角色并授菜单权限(待测试)*/
    @ApiOperation(value="添加角色")
    //参数说明
    @ApiImplicitParams({

            @ApiImplicitParam(name="wmSysRole1" ,value="角色ID" ,required=true ,dataType="WmSysRole",paramType="query"),
            @ApiImplicitParam(name="menuName" ,value="菜单名称" ,required=true ,dataType="String[]",paramType="query") })
    //返回说明
    @ApiResponses({
            @ApiResponse(code=200 , message="执行成功")
    })

    @PutMapping("/addRoleMenu")
    public String addRoleMenu(WmSysRole wmSysRole1,String menuName[]){
        UserDto userDto=new UserDto();
        WmSysRole wmSysRole=new WmSysRole();
        wmSysRole.setRoleName(wmSysRole1.getRoleName());
        userDto.setWmSysRole(wmSysRole);//赋值给dto的角色对象

        List<WmSysMenu> wmSysMenuList=new ArrayList<WmSysMenu>();
        for(String menuName1:menuName){
            WmSysMenu wmSysMenu=new WmSysMenu();
            wmSysMenu.setMenuName(menuName1);
            wmSysMenuList.add(wmSysMenu);
        }
        userDto.setWmSysMenuList(wmSysMenuList);
        return userService.addRoleMenu(userDto);
    }

}
